home *** CD-ROM | disk | FTP | other *** search
/ Aminet 38 / Aminet 38 (2000)(Schatztruhe)[!][Aug 2000].iso / Aminet / util / moni / Scout-src.lha / src / scout_net.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-03-09  |  20.6 KB  |  786 lines

  1. /**
  2.  * Scout - The Amiga System Monitor
  3.  *
  4.  *------------------------------------------------------------------
  5.  *
  6.  * This program is free software; you can redistribute it and/or modify
  7.  * it under the terms of the GNU General Public License as published by
  8.  * the Free Software Foundation; either version 2 of the License, or
  9.  * any later version.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program; if not, write to the Free Software
  18.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  *
  20.  * You must not use this source code to gain profit of any kind!
  21.  *
  22.  *------------------------------------------------------------------
  23.  *
  24.  * @author Andreas Gelhausen
  25.  * @author Richard Körber <rkoerber@gmx.de>
  26.  */
  27.  
  28.  
  29.  
  30. #include "scout_net.h"
  31.  
  32. extern APTR    AP_Scout;
  33. extern STRPTR  _ProgramName;
  34. extern unsigned char * tmpstr;
  35.  
  36. /********************************************************************/
  37. /*                      Benötigte Prototypes                        */
  38. /********************************************************************/
  39.  
  40. struct Library *MyOpenLibrary (char *, ULONG);
  41.  
  42. void PrintAllocations(char *);
  43. void PrintAssigns(char *);
  44. void PrintCommands(char *);
  45. void PrintDevices(char *);
  46. void PrintExpansions(char *);
  47. void PrintFonts(char *);
  48. void PrintInputHandlers(char *);
  49. void PrintInterrupts(char *);
  50. void PrintLibraries(char *);
  51. void PrintMemory(char *);
  52. void PrintMounts(char *);
  53. void PrintLocks(char *);
  54. void PrintPorts(char *);
  55. void PrintResidents(char *);
  56. void PrintSemaphores(char *);
  57. void PrintTasks(char *);
  58. void PrintResources(char *);
  59. void PrintVectors(char *);
  60. void PrintWindows(char *);
  61. void PrintLowMemory(char *);
  62. void PrintCx(char *);
  63. void PrintClass(char *);
  64. void PrintSMode(char *);
  65. void PrintSystem(char *);
  66. void PrintTimer(char *);
  67.  
  68. void SendAlcList(void);
  69. void SendAssList(void);
  70. void SendComList(void);
  71. void SendDevList(void);
  72. void SendExpList(void);
  73. void SendFontList(void);
  74. void SendInputList(void);
  75. void SendIntList(void);
  76. void SendLibList(void);
  77. void SendLockList(void);
  78. void SendMemList(void);
  79. void SendMountList(void);
  80. void SendPortList(void);
  81. void SendResList(void);
  82. void SendResiList(void);
  83. void SendSemList(void);
  84. void SendTaskList(void);
  85. void SendVectorList(void);
  86. void SendWinList(void);
  87. void SendLowMemory(void);
  88. void SendCxList(void);
  89. void SendClassList(void);
  90. void SendSModeList(void);
  91. void SendSystemList(void);
  92. void SendTimerList(void);
  93.  
  94. void PrintNetHelp (void);
  95. long SendResultString (char *fmt, ...);
  96. int MyRequest (char *gadgets, char *fmt, ...);
  97.  
  98. #define NAMELEN      32
  99. #define LINELEN      82
  100. #define TRENNCHAR    '\0'
  101.  
  102. #define aprintf      Printf
  103.  
  104. char  *CMD_BEGIN = "BEGIN";
  105. char  *CMD_USER = "USER";
  106. char  *CMD_PASSWORD = "PASSWORD";
  107. char  *CMD_END = "END";
  108. char  *CMD_DONE = "DONE";
  109. char  *CMD_ERROR = "ERROR";
  110. char  *CMD_CONNECTED = "CONNECTED";
  111.  
  112. static struct  RDArgs      *rdargs;
  113. static struct  RDArgs      *myrdargs;
  114.  
  115. int     server_socket = -1;
  116. int     client_socket = -1;
  117. int     connected = FALSE;
  118. static long    type = SOCK_STREAM, protocol = PF_INET;
  119.  
  120. static long    sinlen;
  121. static struct  sockaddr_in sin;
  122. static struct  sockaddr_in *peer;
  123.  
  124. BOOL clientstate = FALSE;
  125. BOOL serverstate = FALSE;
  126. BOOL shellstate = FALSE;
  127.  
  128. char username[10+1];
  129. char password[_PASSWORD_LEN+1];
  130.  
  131.  
  132. /*
  133. int logprint (char *fmt, ...) {
  134.    int   rc = TRUE;
  135.    char  buf[100];
  136.    char  *logfilename = "basket:scout.log";
  137.    BPTR  handle;
  138.  
  139.    va_list args;
  140.    va_start(args,fmt);
  141.    vsprintf (buf, fmt, args);
  142.  
  143.    if (((handle = Open (logfilename, MODE_OLDFILE)) \
  144.      && (Seek (handle, 0, OFFSET_END) != -1)) \
  145.      || (handle = Open (logfilename, MODE_NEWFILE))) {
  146.       if (Write (handle, buf, strlen (buf)) == -1) {
  147.          rc = FALSE;
  148.       }
  149.       Close (handle);
  150.    }
  151.    va_end (args);
  152.    return (rc);
  153. }
  154. */
  155.  
  156. void failtcp (void) {
  157.    if (client_socket >= 0) {
  158.       if (connected) {
  159.          shutdown (client_socket, 2);
  160.       }
  161.       CloseSocket (client_socket);
  162.    }
  163.  
  164. //   _STD_500_stdio_term();
  165.  
  166.    if (UserGroupBase) CloseLibrary (UserGroupBase);
  167.  
  168. //   _STD_200_closeTimer();
  169.  
  170.    if (SocketBase) CloseLibrary (SocketBase);
  171. }
  172.  
  173. int inittcp (void) {
  174.    SocketBase = OpenLibrary ("bsdsocket.library", 4L);
  175.    if (! SocketBase)
  176.       return (FALSE);
  177.  
  178. //   _STI_200_openTimer();
  179.  
  180.    UserGroupBase = OpenLibrary ("usergroup.library", 4);
  181.    if (! UserGroupBase)
  182.       return (FALSE);
  183.  
  184. //   _STI_500_stdio_init();
  185. //   _STI_510_install_AmiTCP_callback();
  186.  
  187.    return (TRUE);
  188. }
  189.  
  190. static int sgetc (int sock) {
  191.    unsigned char c;
  192.    fd_set rd,ex;
  193.    ULONG flgs;
  194.    int n;
  195.  
  196.    struct timeval t;
  197.    t.tv_sec = 10L;
  198.    t.tv_usec = 0;
  199.  
  200.    FD_ZERO(&rd);
  201.    FD_ZERO(&ex);
  202.  
  203.    FD_SET(sock,&rd);
  204.    FD_SET(sock,&ex);
  205.  
  206.    if (clientstate) {
  207.       flgs = NULL;
  208.       WaitSelect(16,&rd,0L,&ex,&t,&flgs);
  209.    } else {
  210.       flgs = SIGBREAKF_CTRL_C | SIGBREAKF_CTRL_D;
  211.       WaitSelect(16,&rd,0L,&ex,NULL,&flgs);
  212.    }
  213.  
  214.    if (FD_ISSET(sock,&rd)) {
  215.       n = recv(sock, &c, 1, 0);
  216.       if (n == 1) {
  217.          return c;
  218.       } else {
  219.          return -1;
  220.       }
  221.    } else {
  222.       return -1;
  223.    }
  224. }
  225.  
  226. int sgets (int sock, char *string, int maxchars) {
  227.    int   nchars = 0;
  228.    char  c;
  229.  
  230.    while ((nchars < maxchars) && ((c = sgetc(sock)) != EOF) \
  231.      && (c != TRENNCHAR)) {
  232.       if ((!clientstate) && (c == '\n')) {
  233. //         break;
  234.       }
  235.       string[nchars] = c;
  236.       nchars++;
  237.    }
  238.    string[nchars] = '\0';
  239.    return (nchars);
  240. }
  241.  
  242. static char *GetDate (void) {
  243.    time_t   clock = time (NULL);
  244.    return (ctime (&clock));
  245. }
  246.  
  247. /*
  248. static int StripCRLF (char *buffer) {
  249.    int len = strlen (buffer);
  250.    
  251.    while (len && ((buffer[len-1] == '\n') \
  252.      || (len && (buffer[len-1] == '\r')))) {
  253.       buffer[--len] = '\0';
  254.    }
  255.    return (len);
  256. }
  257. */
  258.  
  259. static char    NetHelpText[] = "\n" \
  260.    " Available lists:\n" \
  261.    " ----------------\n" \
  262.    " Allocations     Assigns         BoopsiClasses   Commands\n" \
  263.    " Commodities     Devices         Expansions      Fonts\n" \
  264.    " InputHandlers   Interrupts      Libraries       Locks\n" \
  265.    " LowMemory       Memory          Mounts          Ports\n" \
  266.    " Residents       Resources       ScreenMode      Semaphores\n" \
  267.    " System          Tasks           Timer           Vectors\n" \
  268.    " Windows\n\n" \
  269.    " Other commands:\n" \
  270.    " ---------------\n" \
  271.    " Help";
  272.  
  273. void PrintNetHelp (void) {
  274.    if (serverstate || shellstate) {
  275.       int i = 0;
  276.  
  277.       SendResultString (NetHelpText);
  278.  
  279.       while (arexx_list[i].mc_Name) {
  280.          strcpy (tmpstr, " ");
  281.          strcat (tmpstr, arexx_list[i].mc_Name);
  282.          if (arexx_list[i].mc_Parameters) {
  283.             strcat (tmpstr, " ");
  284.             strcat (tmpstr, arexx_list[i].mc_Template);
  285.          }
  286.          SendResultString (tmpstr);
  287.          i++;
  288.       }
  289.    }
  290. }
  291.  
  292. /*
  293. ** Dieser Hook wird benötigt um die ARexx-Command-Liste für
  294. **  die TCP_Befehle nutzen zu können!
  295. */
  296. struct NetHook {
  297.    struct   MinNode  h_egal1;
  298.    __asm    ULONG    (*h_Entry) (register __a1 ULONG *);
  299.    ULONG    (*h_egal2)();
  300.    APTR     h_egal3;
  301. };
  302.  
  303. struct Net_Command {
  304.    char  *nc_Name;
  305.    void  (*nc_func) (char *);
  306. };
  307.  
  308. struct Net_Command net_list[] = {
  309.    "Allocations",    PrintAllocations,
  310.    "Assigns",        PrintAssigns,
  311.    "BoopsiClasses",  PrintClass,
  312.    "Commands",       PrintCommands,
  313.    "Commodities",    PrintCx,
  314.    "Devices",        PrintDevices,
  315.    "Expansions",     PrintExpansions,
  316.    "Fonts",          PrintFonts,
  317.    "InputHandlers",  PrintInputHandlers,
  318.    "Interrupts",     PrintInterrupts,
  319.    "Libraries",      PrintLibraries,
  320.    "LowMemory",      PrintLowMemory,
  321.    "Memory",         PrintMemory,
  322.    "Mounts",         PrintMounts,
  323.    "Locks",          PrintLocks,
  324.    "Ports",          PrintPorts,
  325.    "Residents",      PrintResidents,
  326.    "ScreenMode",     PrintSMode,
  327.    "Semaphores",     PrintSemaphores,
  328.    "System",         PrintSystem,
  329.    "Tasks",          PrintTasks,
  330.    "Timer",          PrintTimer,
  331.    "Resources",      PrintResources,
  332.    "Vectors",        PrintVectors,
  333.    "Windows",        PrintWindows,
  334.    "Help",           (void (* ) (char *)) PrintNetHelp,
  335.    "a",              PrintAllocations,
  336.    "b",              PrintClass,
  337.    "c",              PrintCommands,
  338.    "d",              PrintDevices,
  339.    "e",              PrintTimer,
  340.    "f",              PrintFonts,
  341.    "g",              PrintAssigns,
  342.    "h",              PrintInputHandlers,
  343.    "i",              PrintInterrupts,
  344.    "j",              PrintLowMemory,
  345.    "k",              PrintCx,
  346.    "l",              PrintLibraries,
  347.    "m",              PrintMemory,
  348.    "n",              PrintMounts,
  349.    "o",              PrintLocks,
  350.    "p",              PrintPorts,
  351.    "r",              PrintResidents,
  352.    "s",              PrintSemaphores,
  353.    "t",              PrintTasks,
  354.    "u",              PrintResources,
  355.    "v",              PrintVectors,
  356.    "w",              PrintWindows,
  357.    "x",              PrintExpansions,
  358.    "y",              PrintSystem,
  359.    "z",              PrintSMode,
  360.    "GetAlcList",     (void (* ) (char *)) SendAlcList,
  361.    "GetAssList",     (void (* ) (char *)) SendAssList,
  362.    "GetClassList",   (void (* ) (char *)) SendClassList,
  363.    "GetComList",     (void (* ) (char *)) SendComList,
  364.    "GetCxList",      (void (* ) (char *)) SendCxList,
  365.    "GetDevList",     (void (* ) (char *)) SendDevList,
  366.    "GetExpList",     (void (* ) (char *)) SendExpList,
  367.    "GetFontList",    (void (* ) (char *)) SendFontList,
  368.    "GetInputList",   (void (* ) (char *)) SendInputList,
  369.    "GetIntList",     (void (* ) (char *)) SendIntList,
  370.    "GetLibList",     (void (* ) (char *)) SendLibList,
  371.    "GetLockList",    (void (* ) (char *)) SendLockList,
  372.    "GetLowMemList",  (void (* ) (char *)) SendLowMemory,
  373.    "GetMemList",     (void (* ) (char *)) SendMemList,
  374.    "GetMountList",   (void (* ) (char *)) SendMountList,
  375.    "GetPortList",    (void (* ) (char *)) SendPortList,
  376.    "GetResList",     (void (* ) (char *)) SendResList,
  377.    "GetResiList",    (void (* ) (char *)) SendResiList,
  378.    "GetSemList",     (void (* ) (char *)) SendSemList,
  379.    "GetSModeList",   (void (* ) (char *)) SendSModeList,
  380.    "GetSysList",     (void (* ) (char *)) SendSystemList,
  381.    "GetTaskList",    (void (* ) (char *)) SendTaskList,
  382.    "GetTimerList",   (void (* ) (char *)) SendTimerList,
  383.    "GetVectorList",  (void (* ) (char *)) SendVectorList,
  384.    "GetWinList",     (void (* ) (char *)) SendWinList,
  385.    NULL
  386. };
  387.  
  388. /********************************************************************/
  389. /*                              Client                              */
  390. /********************************************************************/
  391.  
  392. int SendDaemon (char *fmt, ...) {
  393.    int   result = FALSE;
  394.    char  buf[LINELEN];
  395.  
  396.    va_list args;
  397.    va_start (args,fmt);
  398.    vsprintf (buf, fmt, args);
  399.  
  400.    if (send (client_socket, buf, strlen (buf) + 1, 0) == strlen (buf) + 1) { 
  401.       result = TRUE;
  402.    }
  403.    va_end (args);
  404.    return (result);
  405. }
  406.  
  407. static ULONG NetCommand (char *text) {
  408.    ULONG rc = RETURN_FAIL;
  409.    char  buf[LINELEN];
  410.  
  411.    if (send (client_socket, text, strlen (text) + 1, 0) == strlen (text) + 1) { 
  412. // aprintf ("COMMAND '%ls' send\n", text);
  413.       while (sgets (client_socket, buf, LINELEN)) {
  414.          if (strcmp (buf, CMD_DONE) == 0) {
  415.             rc = RETURN_OK;
  416. // aprintf ("DONE\n");
  417.             break;
  418.          } else if (strcmp (buf, CMD_ERROR) == 0) {
  419. // aprintf ("ERROR\n");
  420.             break;
  421.          }
  422.       }
  423.    }
  424.    return (rc);
  425. }
  426.  
  427. int ReceiveDecodedEntry (UBYTE *structure, int length) {
  428.    int   rc = FALSE, i = 8;
  429.  
  430.    if (sgets (client_socket, structure + 8, length - 8)) {
  431.       if ((strcmp (structure + 8, CMD_ERROR)) \
  432.         && (strcmp (structure + 8, CMD_DONE))) {
  433.          while (i < length - 1) {
  434.             if (structure[i] == '\1')
  435.                structure[i] = '\0';
  436.             i++;
  437.          }
  438.          rc = TRUE;
  439.       }
  440.    }
  441.    return (rc);
  442. }
  443.  
  444. int ConnectToServer (void) {
  445.    char     hostname[NAMELEN];
  446.    char     buffer[LINELEN+2];
  447.    struct   servent     *service;
  448.    struct   hostent     *host;
  449.    struct   sockaddr_in server;
  450.  
  451.    if (! inittcp()) {
  452.       aprintf ("Failed to initialize AmiTCP version 4!\n");
  453.       return (FALSE);
  454.    }
  455.  
  456.    if (opts.User) {
  457.       strncpy (username, opts.User, 10);
  458.    } else {
  459.       strncpy (username, getlogin(), 10);
  460.    }
  461.  
  462.    if (opts.Password) {
  463.       strncpy (password, opts.Password, _PASSWORD_LEN);
  464.    } else {
  465.       strncpy (password, getpass ("Password:"), _PASSWORD_LEN);
  466.       if (password[0] == '\0') {
  467.          aprintf ("Failed to get password!\n");
  468.          return (FALSE);
  469.       }
  470.    }
  471.  
  472.    gethostname (hostname, sizeof (hostname));
  473.    service = getservbyname ("scout","tcp");
  474. //   service = getservbyport ((long) 6543,"tcp");
  475.  
  476.    if (host = gethostbyname (opts.Host)) {
  477.  
  478.       bzero ((char *) &server, sizeof(server));
  479. //      server.sin_port = service->s_port;
  480.       server.sin_port = 6543;
  481.       bcopy (host->h_addr, (char *) &server.sin_addr, host->h_length);
  482.       server.sin_family = host->h_addrtype;
  483.  
  484.       client_socket = socket (AF_INET, type, 0);
  485.       if (client_socket >= 0) {
  486.          if (connect (client_socket, (struct sockaddr *) &server, sizeof (server)) != -1) {
  487.             connected = TRUE;
  488.             if (SendDaemon (CMD_BEGIN)) {
  489.                if ((SendDaemon ("%ls %ls", CMD_USER, username)) \
  490.                  && (SendDaemon ("%ls %ls", CMD_PASSWORD, password))) {
  491.  
  492.                   if ((sgets (client_socket, buffer, LINELEN)) \
  493.                     && (strcmp (buffer, CMD_CONNECTED))) {
  494.                      aprintf (buffer);
  495.                   } else {
  496.                      return (TRUE);
  497.                   }
  498.                }
  499.             }
  500.          } else {
  501.             aprintf ("Couldn't connect to server\n");
  502.          }
  503.       } else {
  504.          aprintf ("Couldn't create socket\n");
  505.       }
  506.    } else {
  507.       aprintf ("Host is not available\n");
  508.    }
  509.    return (FALSE);
  510. }
  511.  
  512. ULONG netshellclient (VOID) {
  513.    ULONG    rc = RETURN_FAIL;
  514.    char     recvbuffer[LINELEN+2];
  515.  
  516.    if (clientstate = ConnectToServer()) {
  517.       if (SendDaemon ("%ls ", opts.Command)) {
  518.  
  519.          while ((sgets (client_socket, recvbuffer, LINELEN)) \
  520.            && (strcmp (recvbuffer, CMD_ERROR)) \
  521.            && (strcmp (recvbuffer, CMD_DONE))) {
  522.             aprintf (recvbuffer);
  523.          }
  524.          if (strcmp (recvbuffer, CMD_DONE) == 0) {
  525.             rc = RETURN_OK;
  526.          } else {
  527.             rc = RETURN_ERROR;
  528.          }
  529.          SendDaemon ("END");
  530.       }
  531.    }
  532.    return (rc);
  533. }
  534.  
  535. /********************************************************************/
  536. /*                              Daemon                              */
  537. /********************************************************************/
  538.  
  539. int isNetCall (void) {
  540.    int   rc = TRUE;
  541.  
  542.    if (! inittcp()) {
  543.       return (FALSE);
  544.    }
  545.  
  546.    server_socket = init_inet_daemon();
  547.    if (server_socket >= 0) {
  548.       set_socket_stdio (server_socket);
  549.       sinlen = sizeof (sin);
  550.       if (getpeername (0, (struct sockaddr *) &sin, &sinlen) == -1) {
  551. //         logprint ("scout: getpeername() failed\n");
  552.          rc = FALSE;
  553.       }
  554.    } else {
  555. //      logprint ("scout: init_inet_daemon() failed\n");
  556.       rc = FALSE;
  557.    }
  558.    return (rc);
  559. }
  560.  
  561. int isCommand (char *buffer, char *command) {
  562.    int len = strlen (command);
  563.    int rc = FALSE;
  564.  
  565.    if ((strnicmp (buffer, command, len) == 0) \
  566.      && ((isspace (buffer[len])) || (buffer[len] == '\0'))) {
  567.       rc = len + 1;
  568.    }
  569.    return (rc);
  570. }
  571.  
  572. int SendClient (char *fmt, ...) {
  573.    int   result = FALSE;
  574.    char  buf[2048];
  575.  
  576.    va_list args;
  577.    va_start(args,fmt);
  578.    vsprintf (buf, fmt, args);
  579.  
  580.    if (send (0, buf, strlen (buf) + 1, 0) == strlen (buf) + 1) { 
  581.       result = TRUE;
  582.    }
  583.    va_end (args);
  584.    return (result);
  585. }
  586.  
  587. int SendEncodedEntry (UBYTE *structure, int length) {
  588.    int   i = 8;
  589.  
  590.    while (i < length - 1) {
  591.       if (structure[i] == '\0')
  592.          structure[i] = '\1';
  593.       i++;
  594.    }
  595.    if (SendClient (structure + 8)) { 
  596.       return (FALSE);
  597.    }
  598.    return (TRUE);
  599. }
  600.  
  601. long SendResultString (char *fmt, ...) {
  602.    char  buf[2048];
  603.  
  604.    va_list args;
  605.    va_start (args,fmt);
  606.    vsprintf (buf, fmt, args);
  607.  
  608.    if (serverstate) {
  609.       strcat (buf, "\n");
  610.       SendClient (buf);
  611.    } else if (shellstate) {
  612.       strcat (buf, "\n");
  613.       Printf (buf);
  614.    } else {
  615.       set (AP_Scout, MUIA_Application_RexxString, buf);
  616.    }
  617.    va_end (args);
  618.    return (TRUE);
  619. }
  620.  
  621. int PrintOneLine (BPTR hd, char *text) {
  622.    if (serverstate) {
  623.       return (SendClient (text));
  624.    } else {
  625.       return (Write (hd, text, strlen (text)) != -1);
  626.    }
  627. }
  628.  
  629. ULONG ExecuteCommand (char *text) {
  630.    ULONG    rc = RETURN_OK;
  631.    char     buffer[256];
  632.    int      i, len = 0;
  633.  
  634.    #define  CMDOPT_TEMPLATE "CMD1/A"
  635.    #define  CMDOPT_COUNT    4
  636.    LONG     cmdopts[CMDOPT_COUNT];
  637.  
  638.    sprintf (buffer, "%ls ", text);
  639.  
  640.    i = 0;
  641.    while (arexx_list[i].mc_Name) {
  642.       if (len = isCommand (buffer, arexx_list[i].mc_Name)) {
  643.  
  644.          if (myrdargs = AllocDosObject (DOS_RDARGS,NULL)) {
  645.             myrdargs->RDA_Source.CS_Buffer = buffer + len;
  646.             myrdargs->RDA_Source.CS_Length = strlen (buffer) - len;
  647.  
  648.             if ((arexx_list[i].mc_Parameters && (rdargs = ReadArgs (arexx_list[i].mc_Template, (LONG *) &cmdopts, myrdargs))) \
  649.               || (!arexx_list[i].mc_Parameters)) {
  650.  
  651.                rc = (*((struct NetHook *) arexx_list[i].mc_Hook)->h_Entry) ((ULONG *) &cmdopts[0]);
  652.                FreeArgs (rdargs);
  653.             } else {
  654.  
  655.                Fault (IoErr(), NULL, (char *) buffer, LINELEN);
  656.                SendResultString (buffer);
  657.                rc = RETURN_FAIL;
  658.             }
  659.             FreeDosObject (DOS_RDARGS,myrdargs);
  660.          }
  661.          break;
  662.       }
  663.       i++;
  664.    }
  665.  
  666.    if (! (arexx_list[i].mc_Name)) {
  667.       i = 0;
  668.       while (net_list[i].nc_Name) {
  669.          if (len = isCommand (buffer, net_list[i].nc_Name)) {
  670.  
  671.             if (myrdargs = AllocDosObject (DOS_RDARGS,NULL)) {
  672.                myrdargs->RDA_Source.CS_Buffer = buffer;
  673.                myrdargs->RDA_Source.CS_Length = strlen (buffer);
  674.  
  675.                if (rdargs = ReadArgs (CMDOPT_TEMPLATE, (LONG *) &cmdopts, myrdargs)) {
  676.  
  677.                   (*net_list[i].nc_func) (NULL);
  678.                   FreeArgs (rdargs);
  679.                } else {
  680.  
  681.                   Fault (IoErr(), NULL, (char *) buffer, LINELEN);
  682.                   SendResultString (buffer);
  683.                   rc = RETURN_FAIL;
  684.                }
  685.                FreeDosObject (DOS_RDARGS,myrdargs);
  686.             }
  687.             break;
  688.          }
  689.          i++;
  690.       }
  691.       if (! (net_list[i].nc_Name)) {
  692.          SendResultString ("Unknown option");
  693.          rc = RETURN_FAIL;
  694.       }
  695.    }
  696.    va_end (args);
  697.    return (rc);
  698. }
  699.  
  700. ULONG netdaemon (VOID) {
  701.    ULONG    rc = TRUE;
  702.    char     buffer[LINELEN+2];
  703.    int      len = 0, done = FALSE;
  704.  
  705.    struct   passwd   *pw, *rootpw;
  706.    long     rootid;
  707.  
  708. /*
  709. ** inittcp() wurde bereits durch isNetCall() aufgerufen!
  710. */
  711.  
  712.    serverstate = TRUE;
  713.  
  714.    if (rootpw = getpwnam ("root"))
  715.       rootid = rootpw->pw_gid;
  716.  
  717.    if ((sgets (0, buffer, LINELEN)) && (strcmp (buffer, CMD_BEGIN) == 0)) {
  718. //logprint ("BEGIN\n");
  719.       if ((sgets (0, buffer, LINELEN)) && (len = isCommand (buffer, CMD_USER)) \
  720.         && (pw = getpwnam (buffer + len))) {
  721. //logprint ("USER %ls\n", buffer + len);
  722.  
  723.          if (((rootpw) && (rootid == pw->pw_gid)) || (! rootpw)) {
  724.  
  725.             if ((sgets (0, buffer, LINELEN)) && (len = isCommand (buffer, CMD_PASSWORD))
  726.               && (strcmp (crypt (buffer + len, pw->pw_passwd), pw->pw_passwd) == 0)) {
  727. //logprint ("PASSWORD %ls\n", buffer + len);
  728.  
  729.                if (SendClient (CMD_CONNECTED)) {
  730. //logprint ("'CONNECTED' send!\n");
  731.                   while (sgets (0, buffer, LINELEN)) {
  732. //logprint ("COMMAND '%ls'\n", buffer);
  733.                      if (strcmp (buffer, CMD_END) == 0) {
  734.                         break;
  735.                      }
  736.  
  737.                      if (ExecuteCommand (buffer)) {
  738.                         SendClient (CMD_ERROR);
  739.                      } else {
  740.                         SendClient (CMD_DONE);
  741.                      }
  742.                      done = TRUE;
  743.                   }
  744.                }
  745.             } else {
  746.                SendClient ("Wrong password\n");
  747.             }
  748.          } else {
  749.             SendClient ("No root privileges\n");
  750.          }
  751.       } else {
  752.          SendClient ("Unknown user\n");
  753.       }
  754.    } else {
  755.       SendClient ("No 'BEGIN' received\n");
  756.    }
  757.    if (! done)
  758.       SendClient (CMD_ERROR);
  759.  
  760.    serverstate = FALSE;
  761.    return (rc);
  762. }
  763.  
  764. int MyDoCommand (char *fmt, ...) {
  765.    int   result = FALSE;
  766.    char  buf[LINELEN];
  767.  
  768.    va_list args;
  769.    va_start(args,fmt);
  770.    vsprintf (buf, fmt, args);
  771.  
  772.    if (clientstate) {
  773.       if (! NetCommand (buf))
  774.          result = TRUE;
  775.    } else {
  776.       if (! ExecuteCommand (buf))
  777.          result = TRUE;
  778.    }
  779.    if (! result) {
  780.       MyRequest ("Continue", "Error occured!");
  781.    }
  782.    va_end (args);
  783.    return (result);
  784. }
  785.  
  786.